package com.coin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.domain.*;
import com.coin.service.*;
import com.coin.mapper.UserWalletMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 李力
 * @description 针对表【user_wallet(用户提币地址)】的数据库操作Service实现
 * @createDate 2025-06-07 10:16:53
 */
@Service
public class UserWalletServiceImpl extends ServiceImpl<UserWalletMapper, UserWallet>
        implements UserWalletService {

    @Autowired
    private UserService userService;

    /**
     * @Description: 分页查询用户的提币地址
     * @Author: 李 力
     * @Date: 2025/6/7
     * @Param:
     * @return: R
     */
    @Override
    public Page<UserWallet> findByPage(Page<UserWallet> page, Long userId) {
        LambdaQueryWrapper<UserWallet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(userId != null, UserWallet::getUserId, userId);
        return page(page, queryWrapper);
    }

    /**
     * @Description: 查询用户某种币的提现地址
     * @Author: 李 力
     * @Date: 2025/6/19
     * @Param:
     * @return: R
     */
    @Override
    public List<UserWallet> findUserWallets(Long userId, Long coinId) {
        LambdaQueryWrapper<UserWallet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(userId != null, UserWallet::getUserId, userId)
                .eq(coinId != null, UserWallet::getCoinId, coinId);
        return list(queryWrapper);
    }

    /**
     * @Description: 新增一个提现地址
     * @Author: 李 力
     * @Date: 2025/6/19
     * @Param:
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveUserWallet(UserWallet userWallet) {
        Long userId = userWallet.getUserId();
        User user = userService.getById(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        String paypassword = user.getPaypassword();
        if (StrUtil.isEmpty(paypassword)) {
            throw new RuntimeException("用户交易密码为空");
        }
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        boolean b = passwordEncoder.matches(userWallet.getPayPassword(), paypassword);
        if (!b) {
            throw new RuntimeException("交易密码错误");
        }
        return save(userWallet);
    }

    /**
     * @Description: deleteAddress
     * @Author: 李 力
     * @Date: 2025/6/19
     * @Param:
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteUserWallet(Long addressId, String payPassword) {
        if (addressId == null || StrUtil.isEmpty(payPassword)) {
            throw new RuntimeException("参数为空");
        }
        UserWallet userWallet = getById(addressId);
        if (userWallet == null) {
            throw new RuntimeException("体现地址错误");
        }
        Long userId = userWallet.getUserId();
        User user = userService.getById(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        boolean b = passwordEncoder.matches(userWallet.getPayPassword(), user.getPaypassword());
        if (!b) {
            throw new RuntimeException("交易密码错误");
        }
        return removeById(addressId);
    }
}




